package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     *分页
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO list(@RequestParam(name = "page") Integer page,
                        @RequestParam(name = "size") Integer size){
        //判断状态是否逻辑删除
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Item::getStatus,3);
        //Page得结果
        Page<Item> pageItem = new Page<>(page,size);
        Page<Item> pageResult = itemService.page(pageItem,queryWrapper);
        //设置到DTO对象
        PageDTO<Item> itemPageDTO = new PageDTO<>();
        itemPageDTO.setTotal(pageResult.getTotal());
        itemPageDTO.setList(pageResult.getRecords());
        //判断状态是否逻辑删除
        return itemPageDTO;
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        Item item = itemService.getById(id);
        return item;
    }

    /**
     * 新增商品
     * @param item
     * @return
     */
    @PostMapping
    public String add(@RequestBody Item item) throws Exception {
        item.setCreateTime(new Date());
        itemService.save(item);
        return "添加成功";
    }

    /**
     * 设置状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public String updateStatus(@PathVariable Long id,
                               @PathVariable int status){
        //设置状态
        Item item = new Item();
        item.setStatus(status);
        item.setUpdateTime(new Date());
        //去和数据库里的数据比较，把对应的修改
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Item::getId,id);
        itemService.update(item,queryWrapper);
        return "成功";
    }
    /**
     * 修改商品
     * @param item
     * @return
     */
    @PutMapping
    public String updateById(@RequestBody Item item){
        item.setUpdateTime(new Date());
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Item::getId,item.getId());
        itemService.update(item,queryWrapper);
        return "修改成功";
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable Long id){
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Item::getId,id);
        Item item = new Item();
        item.setStatus(3);
        item.setUpdateTime(new Date());
        itemService.update(item,queryWrapper);
        return "删除成功";
    }

}
